/*
*	Developed by Leonardo Chatain and Pedro Dusso, at Universidade Federal do Rio Grande do Sul, Brasil, 2009/1
*	Source available at glpacman.codeplex.com
*/

#include "Pacman.h"

Pacman::Pacman(void)
{
	position.setXY(0, 0);
	setDiscreteDirection(Character::up);
	heigthPosition = CELL_SIZE/2;
	lives = 3;
	score = 0;
	super = false;
	turning = false;
	linearSpeed = 1;
	angularSpeed = 5;
	incomingDirection = discreteDirection;
	remainingTime = 0;
	superTime = TIME_SUPER;
	phantomsAte = 0;
}

Pacman::Pacman(float x, float y) {
	Pacman();
	position.setXY(x, y);
}

Pacman::Pacman(Point2D pos) {
	Pacman(pos.getX(), pos.getY());
}

Pacman::Pacman(Point2D newPos, Vector2D dir) {
	Pacman();
	position.setXY(newPos.getX(), newPos.getY());
	direction.setX(dir.getX());
	direction.setY(dir.getY());
	direction.normalize();
}

Pacman::~Pacman(void)
{
}

void Pacman::draw(){
	float maxOffset = 0.3;
	static float originalHeigth = heigthPosition;
	static float increment = 0.07;
	if ((heigthPosition >= originalHeigth + maxOffset) || (heigthPosition <= originalHeigth - maxOffset))
		increment *= -1;

	glPushMatrix();
	
	glTranslatef(position.getX(), heigthPosition, position.getY());

	glColor3f(YELLOW);
	float angle = direction.getAngleTo(Vector2D(1, 0));
	glRotatef(angle, 0, 1, 0);
	glutSolidSphere(CHAR_RADIUS, CHARACTER_SLICES, CHARACTER_STACKS);
	glColor3f(RED);

	glPopMatrix();
	heigthPosition += increment;
}

void Pacman::updateSuper() {
	/*static Point2D lastpos(position.getX(), position.getY());
	if (super) {
		remainingDist -= lastpos.getDistanceTo(position);
		if (remainingDist <= 0) {
			unsetSuper();
		}
	}
	lastpos.setXY(position.getX(), position.getY());*/
	static int lastTime = glutGet(GLUT_ELAPSED_TIME);
	int time = glutGet(GLUT_ELAPSED_TIME);
	if (super) {
		remainingTime = remainingTime - (time - lastTime);
		if (remainingTime <= 0) {
			unsetSuper();
		}
	}
	lastTime = time;
}